<template>
  <div>
    <el-card class="box-card">
      <el-form
        :model="editformData"
        :rules="rules"
        ref="editNewsForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="新闻标题" prop="title">
          <el-input
            v-model="editformData.title"
            placeholder="请输入新闻标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="新闻分类" prop="newsCategoryId">
          <el-select
            placeholder="请选择新闻分类"
            v-model="editformData.newsCategoryId"
          >
            <el-option
              v-for="(item,index) in newsCategories"
              :key="index"
              :label="item.name"
              :value="item.id"
              :selected="editformData.newsCategoryId === item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新闻简介" required prop="describe">
          <el-input
            v-model="editformData.describe"
            placeholder="请输入新闻简介"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="新闻封面" prop="titlePic">
          <el-col :span="12">
            <img :src="baseImgURL+editformData.titlePic" v-show="oldImgShow" style="width:100%" />
          </el-col>
          <el-col :span="12">
            <upload @returnUrl="handleReturnUrl" actionURL="NewsTitlePic"></upload>
            <!-- 在上传成功的回调函数中返回图片src，然后存到一个不显示的input中 -->
            <!-- <input type="text" v-model="editformData.titlePic" v-show="false" /> -->
          </el-col>
        </el-form-item>
        <el-form-item label="正文" prop="content">
          <mavon-editor
            v-model="editformData.content"
            style="min-height: 800px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('editNewsForm')"
            >确认修改</el-button
          >
          <el-button @click="cancelForm">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 导入mavonEditor组件 及 组件样式
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";

import upload from "../../Upload/Upload.vue"

import News from "@/api/News.js"
const news=new News();

export default {
  data() {
    return {
      rules: {
        title: [
          { required: true, message: "请输入新闻标题", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur",
          },
        ],
        category: [
          { required: true, message: "请选择新闻分类", trigger: "blur" },
        ],
        describe: [
          { required: true, message: "请输入新闻简介", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "长度在 1 到 100 个字符",
            trigger: "blur",
          },
        ],
        bigPic: [
          { required: true, message: "请上传新闻封面", trigger: "blur" },
        ],
        content: [
          { required: true, message: "请输入新闻内容", trigger: "blur" },
          {
            min: 1,
            max: 5000,
            message: "长度在 1 到 1000 个字符",
            trigger: "blur",
          },
        ],
      },
      newsCategories: [],

      baseImgURL:'http://121.196.246.105:4000/#/News/TitlePic/',
      oldImgShow:true
    };
  },
  methods: {
    // ··············ChangeMavonContentHandler事件开始···························
    ChangeMavonContentHandler(){

    },

    // ··············ChangeMavonContentHandler事件结束···························


     submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          // 验证通过，开始提交this.editformData
          const token=this.$store.state.token;
          const response=await news.UpdateNews(this.editformData,token)
          if(response.code===200){
            // 操作成功,更新表格数据
            this.$emit("UpdateData");
          }
          else{
            // 操作失败
            this.$message.error("服务器错误，更改失败！")
          }
        } else {
          this.$message.error("输入不正确")
          return false;
        }
      });
    },

    //重置表格
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    //取消按钮，触发隐藏
    cancelForm(){
      this.$emit("cancelForm",false)
    },

    // 上传成功后：由子组件传值返回图片名称
    handleReturnUrl(imageName) {
      // 更新img路径
      this.editformData.titlePic=imageName;
      // 将原来显示Img隐藏
      this.oldImgShow=false;
    },

  },
  components: {
    mavonEditor,
    upload,
  },
  props: {
    editformData: {
      // id: 0,
      // title: "",
      // newsCategoryId: 0,
      // newsCategory: "",
      // describe: "",
      // bigPic: "",
      // content: "",
      type:Object,
      required:true
    },
  },
  async mounted() {
    //   绑定categorys数据
    this.newsCategories = this.$store.state.newsCategories;


    // console.log(this.editformData);
  },
};
</script>